<template>
  <view>
    <u-navbar :title="title"></u-navbar>
    <view class="content">
      <u-search shape="round" v-model="searchValue" @search="init('搜索')" @custom="init('搜索')"></u-search>
      <Loading :loading-status="loadingStatus"></Loading>
      <view v-if="!loadingStatus">
      <no-data v-if="objects.length==0"></no-data>
      <u-cell-group v-else>
        <u-cell-item v-for="(object,index) in objects" :key="index" :arrow="false" :title="object[showName]"  @click="back(object)"></u-cell-item>

        <u-loadmore v-if="this.system != '1'&&loadingMoreShow" :status="status" />
      </u-cell-group>
      </view>
      <!-- 正文内容 -->
    </view>
  </view>
</template>

<script>
import {requestInfo} from "../../api/public";
import noData from "./noData";
import Loading from "./Loading";
export default {
  name: "EntitySelect",
  components:{
    noData,
    Loading
  },
  data(){
    return{
      title:"",
      searchValue:'',
      searchInfo:'',
      queryParams: {currentPage: 1, filters: {}, orderBys: [], pageSize: 15},
      entity:'',
      module:'',
      objects:[],
      system:'',
      showName:'name',
      paramType:'',
      loadingStatus:false,
      status:'loadmore',
      selectObject:{},
      loadingMoreShow:true,
      // totalCount:100,
    }
  },
  onLoad(option){
    const s = decodeURIComponent(option.entity);
    let searchInfo=JSON.parse(s)
    this.module=searchInfo.module
    this.entity=searchInfo.entity

    //提示性标题
    this.title=searchInfo.title===undefined?"":searchInfo.title;

    this.queryFilters=searchInfo.queryFilters
    this.system=searchInfo.system==undefined?'':searchInfo.system
    this.showName=searchInfo.showName==undefined?'name':searchInfo.showName
    this.paramType=searchInfo.paramType==undefined?'':searchInfo.paramType
    this.queryParams.filters=searchInfo.queryFilters==undefined?{}:searchInfo.queryFilters
    // this.queryParams.filters.supplierId = searchInfo.supplierId
    this.queryParams.filters.userId=searchInfo.userId
    this.init()

  },
  methods:{
    init(type) {
      this.queryParams.filters[this.showName]='*'+this.searchValue+'*'
      if(type=='loadingMore') {
        this.status = 'loading'
        console.log("loading")
      }else{
        this.loadingStatus=true
        this.queryParams.currentPage=1
      }
      if (this.system && this.system == '1') {

        requestInfo({
          url: `/${this.module}/${this.entity}`,
          method: 'Get',
        }).then(res => {
            this.objects = res.data.data;
            this.loadingStatus=false
        }).catch(error => {
          console.log(error);
        })
      } else {
        requestInfo({
          url: `/erp/${this.module}/${this.entity}/find${this.entity}s`,
          method: 'POST',
          data: this.queryParams
        }).then(res => {
          // this.totalCount=res.data.totalCount;
          console.log(res.data)
          if(type=='loadingMore') {
            if(res.data.datas.length==0){
              this.status='nomore'
              return
            }
            this.status='loadmore'
            this.objects=this.objects.concat(res.data.datas)
          }else{
            this.objects = res.data.datas;
            if(this.objects.length<this.queryParams.pageSize){
              this.loadingMoreShow=false
              this.status='nomore'
            }else{
              this.loadingMoreShow=true
              this.status='loadmore'
            }
            this.loadingStatus=false
          }
        }).catch(error => {
          console.log(error);
        })
      }
    },
    back(object){
      uni.$emit('updateData',
          {
            object:object,
            type:this.entity,
            paramType:this.paramType
      })
      this.selectObject=object;
      uni.navigateBack({
        delta: 1
      })

    },
    //懒加载
    onReachBottom() {
      // console.log(this.loadingMoreShow)
      this.queryParams.currentPage+=1
      if(this.status=='nomore'){
        return;
      }
      this.init("loadingMore")
    },
  },
  // beforeDestroy () {
  //
  //   uni.$emit('updateData',
  //       {
  //         object:this.selectObject,
  //         type:this.entity,
  //         paramType:this.paramType
  //       })
  // }
}
</script>

<style scoped>

</style>
